home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 48973 / 48973.xpi / chrome / button.js next >
Text File  |  2010-01-28  |  2KB  |  74 lines

  1. var DownloadFolderButton = {
  2.     1: function () {
  3.         var folder;
  4.         
  5.           if (gDownloadsView.selectedItem) {
  6.                 var f = getLocalFileFromNativePathOrUrl(gDownloadsView.selectedItem.getAttribute("file"));
  7.                 
  8.                 try {
  9.                  // Show the directory containing the file and select the file
  10.                  f.reveal();
  11.                } catch (e) {
  12.                  // If reveal fails for some reason (e.g., it's not implemented on unix or
  13.                  // the file doesn't exist), try using the parent if we have it.
  14.                  let parent = f.parent.QueryInterface(Ci.nsILocalFile);
  15.                  if (!parent)
  16.                    return;
  17.              
  18.                  try {
  19.                    // "Double click" the parent directory to show where the file should be
  20.                    parent.launch();
  21.                  } catch (e) {
  22.                    // If launch also fails (probably because it's not implemented), let the
  23.                    // OS handler try to open the parent
  24.                    openExternal(parent);
  25.                  }
  26.                }
  27.  
  28.                  return;
  29.          }
  30.         
  31.         var pref = Components.classes["@mozilla.org/preferences-service;1"]
  32.                             .getService(Components.interfaces.nsIPrefBranch);
  33.         
  34.         var dirService = Components.classes["@mozilla.org/file/directory_service;1"].
  35.                                     getService(Components.interfaces.nsIProperties);
  36.         
  37.         if(pref.getBoolPref("browser.download.useDownloadDir")) {
  38.         // Firefox is going to check where to save by file according to the folderList
  39.             switch (pref.getIntPref("browser.download.folderList")) {
  40.                 case 0: // the desktop
  41.                 var desktop = dirService.get("Desk", Components.interfaces.nsILocalFile);
  42.                 folder = desktop;
  43.                 break;
  44.             case 1: // the downloads folder
  45.                 var dm = Components.classes["@mozilla.org/download-manager;1"]
  46.                    .getService(Components.interfaces.nsIDownloadManager);
  47.  
  48.                 folder = dm.userDownloadsDirectory;
  49.                 
  50.                 break;
  51.             case 2: // the last folder specified for a download
  52.                 folder = pref.getComplexValue("browser.download.dir", Components.interfaces.nsILocalFile);
  53.                 break;
  54.             }
  55.         
  56.         } else {
  57.             // Users are manually pointing out a directory to save to
  58.             folder = pref.getComplexValue("browser.download.lastDir", Components.interfaces.nsILocalFile);
  59.         }
  60.         
  61.         if (folder) {
  62.             try {
  63.                 folder.reveal();
  64.             } catch (ex) {
  65.                 // if nsILocalFile::Reveal failed (eg it currently just returns an
  66.                 // error on unix), just open the folder in a browser window
  67.                 alert(ex);
  68.             }
  69.         } else {
  70.             alert("Unable to open folder, please contact me at wybren@zen-ben.com!");
  71.         }
  72.  
  73.     }
  74. }